﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using Gif.Components;
using System.IO;
using System.IO.IsolatedStorage;

namespace luGIF微电影.View
{
    public partial class FromFilePage : PhoneApplicationPage
    {
        PhotoChooserTask photoChooser = new PhotoChooserTask();
        List<WriteableBitmap> bitmapList = new List<WriteableBitmap>();
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        FileStream gif;
        int photocount = 0;
        bool finished = true;
        public FromFilePage()
        {
            InitializeComponent();
            
            if (photocount == 0) buttonGenerate.IsEnabled = false;
            photoChooser.PixelHeight = 256;
            photoChooser.PixelWidth = 256;
            photoChooser.Completed += new EventHandler<PhotoResult>(photoChooser_Completed);
           // IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

        }

        void photoChooser_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(e.ChosenPhoto);
                Image image = new Image();
                image.Source = bitmap;
                Grid.SetColumn(image, photocount % 4);
                Grid.SetRow(image, photocount / 4);
                image.Margin = new Thickness(0, 0, 5, 0);
                ImageBoard.Children.Add(image);

                bitmapList.Add(new WriteableBitmap(bitmap));
                buttonGenerate.IsEnabled = true;
                photocount++;
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                gif.Close();
            }
            catch(Exception ee) { }
            base.OnBackKeyPress(e);
        }
        private void choosePhoto_click(object sender, RoutedEventArgs e)
        {
            if (photocount < 20 )
            {
                photoChooser.Show();
            }
            else
                MessageBox.Show("只能选择20张图片！");
        }

        private void generate_click(object sender, RoutedEventArgs e)
        {
            if (finished == false)
            {
                String outputFilePath = DateTime.Now.Ticks.ToString() + ".gif";
                gif = encoder.Start(outputFilePath);
                encoder.SetDelay(500);
                encoder.SetRepeat(0);
                foreach (WriteableBitmap w in bitmapList)
                {
                    encoder.AddFrame(w);
                }
                encoder.Finish();
                finished = true;
            }
        }
    }
}